package com.lysj.bhp.activity.controller;

import com.lysj.bhp.activity.base.ActivityContent;
import com.lysj.bhp.activity.domain.Activity;
import com.lysj.bhp.activity.vo.ActivityVo;
import com.lysj.bhp.announcement.domain.Announcement;
import com.lysj.bhp.coinexchange.domain.BhpcRecord;
import com.lysj.bhp.coinexchange.repository.BhpcRecordRepository;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("admin/activity")
public class ActivityController extends ActivityContent {



    @InitBinder
    public void InitBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd hh:mm"), true));
    }

    @GetMapping("/list")
    public Resp list(Activity model) {
        model.setIsExist(1);
        Specification specification = ParamUtil.createSpecification(model);
        List<Activity> activityList = activityRepository.findAll(specification, new Sort(Sort.Direction.DESC, "createDate"));
        return new Resp(activityList);
    }

    @PostMapping("/add")
    public Resp add(Activity model) {
        if (ParamUtil.isBlack(model.getStartDateTime())) {
            return new Resp(Resp.PARAM_ERROR, "请选择活动开始时间");
        }
        if (ParamUtil.isBlack(model.getEndDateTime())) {
            return new Resp(Resp.PARAM_ERROR, "请选择活动结束时间");
        }
        if (ParamUtil.isBlack(model.getChineseName())) {
            return new Resp(Resp.PARAM_ERROR, "请输入中文活动名称");
        }
        if (ParamUtil.isBlack(model.getChinesePhoto())) {
            return new Resp(Resp.PARAM_ERROR, "请上传中文活动图片");
        }
        if (ParamUtil.isBlack(model.getEnglishName())) {
            return new Resp(Resp.PARAM_ERROR, "请输入英文活动名称");
        }
        if (ParamUtil.isBlack(model.getEnglishPhoto())) {
            return new Resp(Resp.PARAM_ERROR, "请上传英文活动图片");
        }
        model.setCreateDate(new Date());
        model.setIsExist(1);
        model.setStatus(1); // 活动未开始
        activityRepository.save(model);
        return new Resp("添加成功");
    }

    @PostMapping("/edit")
    public Resp edit(Activity model) {
        Activity activity = activityRepository.findOne(model.getActivityId());
        if (activity == null) {
            return new Resp(Resp.PARAM_ERROR, "活动不存在!");
        }
        if (ParamUtil.isBlack(model.getStartDateTime())) {
            return new Resp(Resp.PARAM_ERROR, "请选择活动开始时间");
        }
        if (ParamUtil.isBlack(model.getEndDateTime())) {
            return new Resp(Resp.PARAM_ERROR, "请选择活动结束时间");
        }
        if (ParamUtil.isBlack(model.getChineseName())) {
            return new Resp(Resp.PARAM_ERROR, "请输入中文活动名称");
        }
        if (ParamUtil.isBlack(model.getChinesePhoto())) {
            return new Resp(Resp.PARAM_ERROR, "请上传中文活动图片");
        }
        if (ParamUtil.isBlack(model.getEnglishName())) {
            return new Resp(Resp.PARAM_ERROR, "请输入英文活动名称");
        }
        if (ParamUtil.isBlack(model.getEnglishPhoto())) {
            return new Resp(Resp.PARAM_ERROR, "请上传英文活动图片");
        }
        activity.setStartDateTime(model.getStartDateTime());
        activity.setEndDateTime(model.getEndDateTime());
        activity.setChineseName(model.getChineseName());
        activity.setChinesePhoto(model.getChinesePhoto());
        activity.setChineseIntroduce(model.getChineseIntroduce());
        activity.setChineseBigPhoto(model.getChineseBigPhoto());
        activity.setEnglishName(model.getEnglishName());
        activity.setEnglishPhoto(model.getEnglishPhoto());
        activity.setEnglishBigPhoto(model.getEnglishBigPhoto());
        activity.setEnglishIntroduce(model.getEnglishIntroduce());
        activityRepository.save(activity);
        return new Resp("修改成功");
    }


    @PostMapping("/edit_chinese_detail")
    public Resp editChineseDetail(Long activityId, String chineseDetail) {
        Activity activity = activityRepository.findOne(activityId);
        if (activity == null) {
            return new Resp(Resp.PARAM_ERROR, "请选择活动");
        }
        if (ParamUtil.isBlack(chineseDetail)) {
            chineseDetail = "";
        }
        activity.setChineseDetail(chineseDetail);
        activityRepository.save(activity);
        return new Resp("保存成功");
    }


    @PostMapping("/remove")
    public Resp remove(Activity model) {
        if (ParamUtil.isBlack(model.getActivityId())) {
            return new Resp(Resp.PARAM_ERROR, "请选择活动");
        }
        Activity  activity = activityRepository.findOne(model.getActivityId());
        if (null == activity) {
            return new Resp(Resp.PARAM_ERROR, "查无此活动");
        }
        activity.setIsExist(2); //设定状态为2,用于逻辑删除
        activityRepository.save(activity);
        return new Resp("删除成功");
    }


    @PostMapping("/edit_english_detail")
    public Resp editEnglishDetail(Long activityId, String englishDetail) {
        Activity activity = activityRepository.findOne(activityId);
        if (activity == null) {
            return new Resp(Resp.PARAM_ERROR, "请选择活动");
        }
        if (ParamUtil.isBlack(englishDetail)) {
            englishDetail = "";
        }
        activity.setEnglishDetail(englishDetail);
        activityRepository.save(activity);
        return new Resp("保存成功");
    }




}
